﻿package KK.Loader
{ 
	
	import KK.GameBase.Events.SceneEvent;
	import KK.GameBase.IEventBase;
	import KK.GameBase.IMessageFollowers;
	import KK.GameBase.IMessageQueue;
	import KK.GameBase.MSGEvent;
	import KK.GameBase.SpriteBase;
	import KK.System.System;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;

	public  class Loading extends Sprite implements IMessageFollowers, IEventBase
	{
		static public const LoaderProgress:String=String(0xFFF009);
		static public const Loaded:String=String(0xFFF010);
		
		static public const SHOW:String=String(0xFFF001);
		static public const HIDE:String=String(0xFFF002);
		 
		 
		protected var loading:MovieClip;
		public var status:int;
		
		private var _mainEvent:IMessageQueue;
		
		public function Loading()
		{
			init();
		}
		/**
		 * 绑定事件对象 
		 * @事件分发器 event
		 * 
		 */		
		public function BindEvent(_event:IMessageQueue):Boolean{
			this._mainEvent=_event;
			initBind();
			return true;
		}
		private function init():void
		{
			status =1;
		}
		private function initBind():void
		{
			
			//AttentionMessage(LoaderProgress);
			AttentionMessage(SHOW);
			AttentionMessage(HIDE);
		}
		/**
		 * 设置并加载本类UI 
		 * @param url
		 * 
		 */		
		public function SetUIUrl(url:String):void{
			
			System.AddSwfLoader(url,complete,null,IOError,SecurityError,Progress);
		}
		
		protected function IOError(event:Event):void
		{
			throw new Error("Loading loader error");
		}
		protected function SecurityError(event:Event):void{
		    throw new Error("Loading SecurityError");
		}
		protected function Progress(event:Event):void
		{
		}
		protected function complete(event:Event):void
		{
			loading=event.target.content;
			
			addChild(loading);
//			Center(loading);
			UILoaded();
			
		}
		
		protected function Center(mc:DisplayObjectContainer):void
		{
			if(mc.stage)
			{
				mc.x=0;
				mc.y=0;
				mc.x=mc.stage.stageWidth/2-mc.width/2;
				mc.y=mc.stage.stageHeight/2-mc.height/2;
			}
		}
		
		/**
		 * UI加载完毕 
		 * 
		 */
		protected function UILoaded():void
		{
			
			status=0;
			Publish(new MSGEvent(String(Loaded)));
		}
		
		public function set Percent(value:Number):void{
			loading.Percent=value;
		}
		public function get Percent():Number{
			return loading.Percent;
		}
		protected function Publish(msg:MSGEvent):void{
			if(_mainEvent)
			{
				_mainEvent.Publish(msg);
			}
		}
		protected function AttentionMessage(msg:String):void
		{
			if(_mainEvent!=null)
			{
				_mainEvent.AttentionMessage(msg,this);
			}
		}
		
		
		override public function set x(value:Number):void{
			if(loading){
				loading.x=value;	
			}
		}
		override public function get x():Number{
			if(loading)
			{
				return loading.x;
			}else
			{
				return 0;
			}
		}
		override public function set y(value:Number):void{
			if(loading)
			{
				loading.y=value;
			}
		}
		override public function get y():Number{
			if(loading)
			{
				return loading.y;
			}else
			{
				return this.y;
			}
		}
		override public function get width():Number{
			if(loading)
			{
				return loading.width;
			}else{
				return this.width;
			}
		}
		override public function set width(value:Number):void{
			if(loading)
			{
				loading.width=value;
			}
		}
		override public function get height():Number{
			if(loading){
				return loading.height;
			}else{
				return this.height;
			}
		}
		override public function set height(value:Number):void{
			if(loading){
				loading.height=value;
			}
		}
		
		public function Update(event:MSGEvent):void{
			switch(event.type)
			{
				case LoaderProgress:
					Percent=MProgressEvent(event).Percent;
					break;
				case SHOW:
					this.visible=true;
					break;
				case HIDE: 
					this.visible=false;
					break; 
			}
			
		}
	}
}